import Vue from 'vue';
import VueRouter from 'vue-router';

import Login from '@/components/Login.vue';
import Home from '@/components/Home.vue';
import Welcome from '@/components/Welcome.vue';
import Users from '@/components/user/Users.vue';
import StorageCell from "@/components/storageCell/StorageCell.vue"
import StorageCellFuhe from "@/components/storageCell/StorageCellFuhe.vue"
import StorageCellUnmake from "@/components/storageCell/StorageCellUnmake.vue"


Vue.use(VueRouter)

const router = new VueRouter({
    routes: [
        // 重定向
        { path: '/', redirect: '/login' },
        // 登录组件
        { path: '/login', component: Login },
        // 后台主页组件
        // { 
        //     path: '/home', 
        //     component: Home,
        //     redirect: '/home/welcome',
        //     children: [
        //         { path: 'welcome', component: Welcome }
        //     ]
        // },
        { 
            path: '/home', 
            component: Home,
            redirect: '/welcome',
            children: [
                { path: '/welcome', component: Welcome },
                { path: '/users', component: Users },
				{ path: '/StorageCell',component:StorageCell},
				{ path: '/StorageCellFuhe',component:StorageCellFuhe},
				{ path: '/StorageCellUnmake',component:StorageCellUnmake},
            ]
        },
       
    ]
})

// 全局导航守卫
router.beforeEach((to, from, next)=>{
    // 如果访问的路由是/login, 不判断直接放行
    if(to.path === '/login')
        return next()
    // 如果访问的路由不是/login, 判断是否有token
    // 从sessionStorage中获取token
    const token = window.sessionStorage.getItem('token')
    if(!token)
        return next('/login')
    next()
})

export default router